<template>
    <div class="cash-detail-box" v-loading="loading">
        <div class="input-box">
            <span class="input-title">时间段</span>
            <el-date-picker
                @change="getList(1)"
                v-model="date"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>

            <span style="margin:0 10px 0 15px;">用户ID</span>
            <el-input
                placeholder="请输入用户ID"
                @keyup.enter.native="getList(1)"
                style="width:180px;"
                clearable
                v-model.trim="userId">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <span style="margin:0 10px 0 10px;">提现状态</span>
            <el-select @change="getList(1)" v-model="applyStatus" placeholder="请选择" style="width:120px;">
                <el-option label="全部" :value="0"></el-option>
                <el-option label="待处理" :value="1"></el-option>
                <el-option label="提现中" :value="2"></el-option>
                <el-option label="已到账" :value="3"></el-option>
                <el-option label="提现失败" :value="4"></el-option>
            </el-select>
        </div>

        <div class="input-box" style="margin-top:10px;">
            <span class="input-title">当日金额</span>
            <el-input-number controls-position="right" style="width:160px;" v-model="fromMoney" :min="0"></el-input-number>
            <span style="margin: 0 10px;">至</span>
            <el-input-number controls-position="right" style="width:160px;" v-model="toMoney" :min="0"></el-input-number>

            <span class="input-title" style="margin:0 10px 0 10px;">累计金额</span>
            <el-input-number controls-position="right" style="width:160px;" v-model="fromTotalMoney" :min="0"></el-input-number>
            <span style="margin: 0 10px;">至</span>
            <el-input-number controls-position="right" style="width:160px;" v-model="toTotalMoney" :min="0"></el-input-number>
            <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
        </div>

        <div class="table-box">
            <el-table
                :data="list"
                :span-method="arraySpanMethod"
                border
                style="width: 100%">
                <el-table-column
                    width="50"
                    fixed
                    label="排序">
                    <template slot-scope="scope">
                        <div class="item-td">{{scope.$index + 1}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    min-width="200"
                    label="用户信息">
                    <template slot-scope="scope">
                        <div class="item-td">
                            <div>
                                <b>用户昵称：</b>
                                <span>{{scope.row.userName}}</span>
                            </div>
                            <div>
                                <b>用户ID：</b>
                                <span class="table-btn" @click="goUserDetail(scope.row.userId)">{{scope.row.userId}}</span>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="todayMoney"
                    sortable
                    width="105"
                    label="今日提现">
                    <template slot-scope="scope">
                        <div class="item-td">
                            <span>{{scope.row.todayMoney || '0'}}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="money"
                    sortable
                    width="105"
                    label="累计提现">
                    <template slot-scope="scope">
                        <div class="item-td">
                            <span>{{scope.row.money || '0'}}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="100"
                    label="提现金额">
                    <template slot-scope="scope">
                        <div class="for-items" v-for="(item, index) in scope.row.withDrawDayDetailList" :key="index">
                            <div class="items-column" style="width:100px;">{{item.money || '0'}}</div>
                            <div class="items-column" style="flex:1;width:200px">
                                <div>
                                    <div class="hidden">
                                        <b>真实姓名：</b>
                                        <span>{{item.fullName || '-'}}</span>
                                    </div>
                                    <div>
                                        <b>提现方式：</b>
                                        <span v-if="item.status === '1'">微信</span>
                                        <span v-else>支付宝</span>
                                    </div>
                                    <div>
                                        <!-- "提现状态 1为待处理 2为提现中 3为已到账 4为提现失败" -->
                                        (<span v-if="item.applyStatus === 1">待处理</span>
                                        <span style="color:#909399;" v-else-if="item.applyStatus === 2">提现中</span>
                                        <span style="color:#67C23A" v-else-if="item.applyStatus === 3">已到账</span>
                                        <span v-else>
                                            <span style="color:#F56C6C">提现失败</span>
                                            <span v-if="item.failReason">：{{item.failReason}}</span>
                                        </span>)
                                    </div>
                                </div>
                            </div>
                            <div class="items-column" style="width:160px;">{{initTime(item.withdrawTime)}}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="200"
                    label="提现账号">
                </el-table-column>
                <el-table-column
                    width="160"
                    label="提现时间">
                </el-table-column>
                <el-table-column
                    min-width="160"
                    label="收入来源">
                    <template slot-scope="scope">
                        <div class="item-td" v-if="scope.row.applySource">
                            <div v-if="scope.row.applySource.rewardSource">
                                <b>打赏：</b>
                                <span>{{scope.row.applySource.rewardSource}}金币</span>
                            </div>
                            <div v-if="scope.row.applySource.taskSource">
                                <b>任务：</b>
                                <span>{{scope.row.applySource.taskSource}}金币</span>
                            </div>
                            <div v-if="scope.row.applySource.inviteSource">
                                <b>邀请：</b>
                                <span>{{scope.row.applySource.inviteSource}}趣宝石</span>
                            </div>
                            <div v-if="scope.row.applySource.historySource">
                                <b>历史邀请：</b>
                                <span>{{scope.row.applySource.historySource}}金币</span>
                            </div>
                            <div v-if="scope.row.applySource.total">
                                <b>合计：</b>
                                <span>{{scope.row.applySource.total}}元</span>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    fixed="right"
                    width="100"
                    label="操作">
                    <template slot-scope="scope">
                        <div class="placeholder-for-box">
                            <div class="placeholder-for-items" v-for="(item, index) in scope.row.withDrawDayDetailList" :key="index">
                                <div class="hidden">
                                    <b>真实姓名：</b>
                                    <span>{{item.fullName || '-'}}</span>
                                </div>
                                <div class="hidden">
                                    <b>提现方式：</b>
                                    <span v-if="item.status === '1'">微信</span>
                                    <span v-else>支付宝</span>
                                </div>
                                <div class="hidden">
                                    <!-- "提现状态 1为待处理 2为提现中 3为已到账 4为提现失败" -->
                                    (<span v-if="item.applyStatus === '1'">待处理</span>
                                    <span style="color:#909399;" v-else-if="item.applyStatus === '2'">提现中</span>
                                    <span style="color:#67C23A" v-else-if="item.applyStatus === '3'">已到账</span>
                                    <span v-else>
                                        <span style="color:#F56C6C">提现失败</span>
                                        <span v-if="item.failReason">：{{item.failReason}}</span>
                                    </span>)
                                </div>

                                <div class="btn-box" v-if="item.applyStatus === 1 || item.applyStatus === 2">
                                    <div v-if="item.applyStatus === 1"><el-button type="text" size="small" @click="beforeUpdateStatus(item, 4)">通过</el-button></div>
                                    <div v-if="item.applyStatus === 1 || item.applyStatus === 2" @click="beforeUpdateStatus(item, -3)"><el-button style="color:red;" type="text" size="small">拒绝</el-button></div>
                                </div>
                                <div v-else class="btn-box" style="color:#67C23A;">已处理</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 提现不通过 -->
        <el-dialog
            class="show-refused-user-box"
            title="提现不通过"
            :close-on-click-modal="false"
            :show-close="false"
            :visible.sync="showRefusedUser"
            width="600px">
            <el-radio-group v-model="radioTxt">
                <el-radio-button class="radio-item" label="怀疑机器人作弊">怀疑机器人作弊</el-radio-button>
                <el-radio-button class="radio-item" label="账号异常">账号异常</el-radio-button>
            </el-radio-group>
            <el-input
                ref="textarea"
                type="textarea"
                :rows="2"
                placeholder="手动输入下架原因"
                v-model="textarea">
            </el-input>
            <span style="color:red;" v-if="!radioTxt && !textarea">请输入或选择提现不通过原因</span>
            <span slot="footer" class="dialog-footer">
                <el-button size="mini" @click="showRefusedUser = false">取 消</el-button>
                <el-button v-if="radioTxt || textarea" size="mini" type="primary" @click="updateStatus(-3)">确定下架</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data  () {
        return {
            date: [], // 选中的日期
            userId: '',
            fromMoney: 0,
            toMoney: 100000,
            fromTotalMoney: 0,
            toTotalMoney: 100000,
            applyStatus: 0,
            list: [],
            loading: false,
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            showRefusedUser: false, // 拒绝用户
            textarea: '', // 拒绝原因（文本框）
            radioTxt: '怀疑机器人作弊' // 拒绝原因（选择）
        }
    },

    created () {
        this.getList(1)
    },
    watch: {
        textarea (val) {
            if (val) {
                this.radioTxt = ''
            }
        },
        radioTxt (val) {
            if (val) {
                this.textarea = ''
            }
        }
    },

    methods: {
        async getList (num) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num
            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                applyStatus: this.applyStatus
            }
            if (this.date && this.date.length) {
                params.fromDate = this.date[0]
                params.toDate = this.date[1]
            }
            if (this.fromMoney !== '') {
                params.fromMoney = this.fromMoney
            }

            if (this.toMoney !== '') {
                params.toMoney = this.toMoney
            }
            if (this.fromTotalMoney !== '') {
                params.fromTotalMoney = this.fromTotalMoney
            }

            if (this.toTotalMoney !== '') {
                params.toTotalMoney = this.toTotalMoney
            }

            if (this.userId) {
                params.userId = this.userId
            }

            let res = await this.$http.post('/withdraw/detail', params)
            if (res.code === '0') {
                this.list = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        // 时间转换
        initTime (time) {
            if (time) {
                return formatDate(new Date(time), 'YYYY-MM-DD HH:MM')
            } else {
                return '-'
            }
        },

        // 查看用户详情
        goUserDetail (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        // 页容切换
        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        // 页码切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        beforeUpdateStatus (item, status) {
            this.currentItem = item
            // 拒绝
            if (status === -3) {
                this.showRefusedUser = true
                // 通过
            } else {
                this.$confirm('此操作将直接给用户打款, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.updateStatus(status)
                }).catch(() => {
                })
            }
        },

        async updateStatus (status) {
            let params = {
                recordId: this.currentItem.recordId,
                status: status
            }
            if (status === -3) {
                params.failReason = this.radioTxt || this.textarea
            }
            let res = await this.$http.post('/withdraw/updateStatus', params)
            if (res.code === '0') {
                this.showRefusedUser = false
                this.currentItem.applyStatus = status === 4 ? 2 : 4
                this.currentItem.failReason = this.radioTxt || this.textarea
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.showRefusedUser = false
        },

        // 合并单元格
        arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 4) {
                return [1, 3]
            } else if (columnIndex === 5 || columnIndex === 6) {
                return [0, 0]
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .cash-detail-box {
        .input-box {
            font-size: 13px;
            font-weight: 500;
            .btn {
                margin-left: 15px;
            }
            .input-title {
                display: inline-block;
                width: 60px;
            }
        }
        .table-box {
            margin: 20px 0;
            .table-btn {
                color: #409EFF;
                cursor: pointer;
                &:hover {
                    text-decoration: underline;
                }
            }

            /deep/ td {
                padding: 0;
                .cell {
                    padding: 0;
                }
            }
            .item-td {
                padding: 12px 10px;
            }

            .for-items {
                display: flex;
                border-bottom: 1px solid #EBEEF5;
                &:last-child {
                    border: none;
                }
                .items-column {
                    padding: 12px 10px;
                    display: flex;
                    align-items: center;
                    border-right: 1px solid #EBEEF5;
                    &:last-child {
                        border: none;
                    }
                }
            }
            .placeholder-for-box {
                width: 100px;
                overflow: hidden;
            }
            .placeholder-for-items {
                width: 200px;
                position: relative;
                top: 0;
                left: 0;
                z-index: 1;
                padding: 12px 10px;
                border-bottom: 1px solid #EBEEF5;
                &:last-child {
                    border: none;
                }
                .hidden {
                    opacity: 0;
                }
                .btn-box {
                    position: absolute;
                    top: 50%;
                    left: 10px;
                    z-index: 1;
                    transform: translateY(-50%);
                }
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }

        .show-refused-user-box {
            /deep/ .el-dialog__body{
                padding: 0 20px;
            }
            .radio-item {
                border: 1px solid #DCDFE6;
                margin-right: 10px;
                margin-bottom: 10px;
                /deep/ .el-radio-button__inner {
                    border: none;
                }
            }
        }
    }
</style>
